package com.ying.dogying.templates;

import com.ying.dogying.templates.common.TemplateDirective;
import com.ying.dogying.templates.common.TemplateDto;
import com.ying.dogying.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Component;

import java.util.*;

@Component
public class ImHotsTemplate extends TemplateDirective {

    @Autowired
    private RedisUtils redisUtils;

    @Override
    public String getName() {
        return "hots";
    }

    @Override
    public void execute(TemplateDto templateDto) throws Exception {
        Set<ZSetOperations.TypedTuple> zSetRank = redisUtils.getZSetRank("post:week:", 0, 6);
        List<Map<String, Object>> hotList = new ArrayList<>();
        for (ZSetOperations.TypedTuple typedTuple : zSetRank) {
            Map<String, Object> map = new HashMap<>();
            String key = "post:data:" + typedTuple.getValue();
            map.put("id", redisUtils.hget(key, "post:id:"));
            map.put("title", redisUtils.hget(key, "post:title:"));
            map.put("commentCount", typedTuple.getScore());
            map.put("viewCount", redisUtils.hget(key, "post:viewCount:"));
            hotList.add(map);
        }
        templateDto.put(RESULTS, hotList).render();
    }
}
